<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 18-12-18
 * Time: 下午4:28
 */

namespace commen;


use think\Log;

class ifuly
{
    private static $url = "https://hzy.ifuly.cn/";
    private static $m_id = "3";
    private static $key = "byyeWdPsVnQmceog";

    static function get_balance()
    {
        return self::get_curl("api/queryBalace", []);
    }

    static function get_order($order_no)
    {
        return self::get_curl("api/queryOrder", ["trade_no"=>$order_no]);
    }
    static function recharge($order_no,$card_no,$amount)
    {
        return self::get_curl("api/mcharge", ["trade_no"=>$order_no,"fuel_card_no"=>$card_no,"card_money"=>$amount,"is_fast"=>1]);
    }
    static function get_curl($action, $params=[])
    {
        $headers = ['Content-Type: application/json;charset=utf-8'];
        $url = self::$url . $action;
        $ts = NOW_TIME;
        $sign_str= self::$m_id.$ts.self::$key;
        $params["sign"] = md5($sign_str);
        $params["m_id"] = self::$m_id;
        $params["ts"] = $ts;
        $info = ["error" => 1, "error_info" => "", "data" => [],"raw_post_data"=>""];
        Log::log(json_encode([$url, $headers]));
        $result= self::curl_url($url, $headers,json_encode($params,JSON_UNESCAPED_UNICODE));
        Log::log(json_encode($result));
        $info["raw_post_data"] = json_encode($result);
        if ($result["code"] == 200) {
            $body = json_decode($result["body"], true);
            if(empty($body))
                $info["error_info"] = $result["error_info"];
            else{
                $error = isset($body["error"])?intval($body["error"]):-1;
                $msg = !empty($body["msg"])?$body["msg"]:"";
                $data = !empty($body["data"])?$body["data"]:[];
                if($error == 0)
                {
                    $info["error"] = 0;
                    $info["data"] = $data;
                }else{
                    $info["error_info"] = $msg;
                }

            }

        } else {
            $info["error_info"] = $result["error_info"];
        }
        return $info;
    }

    static function curl_url($url, $headers=[], $body=[])
    {
        $result = ["code" => 1, "body" => "", "error_info" => ""];
        try {
            $ch = curl_init();
            //设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            if (!empty($headers))
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            if (!empty($body)) {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            }
            //执行并获取HTML文档内容
            $output = curl_exec($ch);
//            print_r($output);
            if ($output === false) {
                $result["error_info"] = curl_errno($ch);
            }
            $result["code"] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $result["body"] = $output;
            //释放curl句柄
            curl_close($ch);
        } catch (\Exception $e) {
            $result["error_info"] = $e->getMessage();
        }
        return $result;
    }

}